package com.tianji.learning.service.impl;

import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseSimpleInfoDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.mapper.LearningLessonMapper;
import com.tianji.learning.service.ILearningLessonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学生课程表 服务实现类
 * </p>
 *
 * @author dmx
 * @since 2024-12-07
 */
@Service
@RequiredArgsConstructor
public class LearningLessonServiceImpl extends ServiceImpl<LearningLessonMapper, LearningLesson> implements ILearningLessonService {

    //导入课程远程调用的客户端
    private final CourseClient courseClient;


    /**
     * 添加课程
     * @param userId
     * @param courseIds
     */
    @Override
    @Transactional
    public void addUserLessons(Long userId, List<Long> courseIds) {

        List<CourseSimpleInfoDTO> simpleInfoList = courseClient.getSimpleInfoList(courseIds);
        if (BeanUtils.isEmpty(simpleInfoList)){
            return;
        }
        List<LearningLesson> learningLessonList =new ArrayList<>();
        //构建lesson对象并存入集合中
        for (CourseSimpleInfoDTO courseSimpleInfoDTO : simpleInfoList) {
            LearningLesson learningLesson = new LearningLesson();
            learningLesson.setCourseId(courseSimpleInfoDTO.getId());
            learningLesson.setUserId(userId);//获取用户的id
            learningLesson.setExpireTime(LocalDateTime.now().plusMonths(courseSimpleInfoDTO.getValidDuration()));
            learningLessonList.add(learningLesson);
        }
        //向数据库中批量插入数据
        this.saveBatch(learningLessonList);
    }


    /**
     * 分页查询我的课程
     * @param pageQuery
     * @return
     */
    @Override
    public List<LearningLessonVO> pageQuery(PageQuery pageQuery) {
        //1.分页查询出结果
        Long user = UserContext.getUser();

        //System.out.println(user);
        //2.调用远程调用查询课程的接口查询相关的数据

        //3.将查询到的结果封装为vo

        //4.返回




        return null;
    }
}
